home *** CD-ROM | disk | FTP | other *** search
- /*
- CLUTLess
-
- In some instances it is desireable to store picts stripped of
- CLUTs in order to save some room in the disk. This sample shows
- how to create such picts and how to properly display them back.
-
- SimpleInC.c -- initialization stuff and event loop
- CLUTLess.c -- code that does the creation of the clut less pict;
- shows also how to display the pictures.
- CLUTLessFile.c --
- Routines to access files that have no direct
- relationship with picture handling.
-
- */
- #include "CLUTLessFile.h"
-
- /* =============================================================================================== */
- /* =============================================================================================== */
- /* Procs used to save the pict file and the clut
- /* =============================================================================================== */
-
- /* The pict does not have a color table in its pixmap so here we save
- the clut and we'll use it later when displaying the image.
- */
- void SaveCLUTResource(SFReply *sfr)
- {
- short resNum, currRes;
-
- CreateResFile((sfr)->fName);
- if (resNum = ResError()) return;
-
- currRes = CurResFile();
-
- if ( (resNum = (HOpenResFile((sfr)->vRefNum, 0, (sfr)->fName, fsRdWrPerm))) == -1) {
- resNum = ResError();
- goto bail;
- }
- else {
- UseResFile(resNum);
- AddResource((Handle)gSharedClut, 'clut', 128, "\p");
- CloseResFile(resNum);
- }
- bail:
- UseResFile(currRes);
- }
-
- short OpenPictureFile(SFReply *reply) /* returns refNum for file */
- {
- long myCount = 4;
- long myZero = 0;
- short ref = 0,result = 0, i;
-
- if ( Create(reply -> fName, reply -> vRefNum, '????', 'PICT') ) goto bail;
-
- if ( FSOpen(reply -> fName, reply -> vRefNum, &ref) )
- {
- FSDelete(reply -> fName,reply -> vRefNum);
- goto bail;
- }
-
- for (i = 1; i<= (532/4); i++)
- {
- if ( FSWrite(ref, &myCount, (Ptr)&myZero) )
- {
- KillFile(ref,reply);
- goto bail;
- }
- }
-
- if ( SetFPos( ref, fsFromStart, 512) )
- {
- KillFile(ref,reply);
- goto bail;
- }
- result = ref;
- bail:
- return result;
- }
-
- void KillFile(short ref, SFReply *reply)
- {
- FSClose(ref);
- FSDelete(reply -> fName,reply -> vRefNum);
- return;
-
- }
-
-
- /* Before closing we need to write to disk the header of the picture */
- void ClosePictureFile(short ref, SFReply *reply, PicHandle aPict)
- {
- long pictHeaderSize = 10;
- if ( SetFPos( ref, fsFromStart, 512) )
- {
- KillFile(ref,reply);
- return;
- }
- HLock((Handle) aPict);
- if ( FSWrite( ref, (long *)&pictHeaderSize, (Ptr)*aPict) )
- {
- KillFile(ref,reply);
- return;
- }
- HUnlock((Handle) aPict);
-
- if ( FSClose( ref) )
- {
- KillFile(ref,reply);
- return;
- }
- }
-
- /* =============================================================================================== */
- /* =============================================================================================== */
- /* Procs used to get back the pict file and the clut
- /* =============================================================================================== */
-
- /* Now we read in the picture and its clut */
- Boolean GetPictBack(void) {
- Point dlgPos = {100,100}; /* Position the dialog box */
- SFReply sfr; /* StdFile reply */
- SFTypeList tList = {'PICT'}; /* only do pict files */
- short fileRef;
- Boolean result = false;
- long curEOF;
- PicHandle pH;
-
- /* Prompt the user for file name to read from */
- SFGetFile(dlgPos, nil, nil, 1, tList, nil, &sfr);
- if (sfr.good) {
- if (!(FSOpen(sfr.fName,sfr.vRefNum, &fileRef)) ) {
- if (!(GetEOF(fileRef, &curEOF) )) {
- curEOF -= 512; // jump over pict file header
- if ( !(SetFPos( fileRef, fsFromStart, 512)) ) {
- if ( pH = (PicHandle)NewHandle(curEOF) ) { // allocate space for picture
- HLock((Handle)pH);
- if ( FSRead(fileRef, &curEOF, *pH) ) {
- HUnlock((Handle)pH);
- DisposeHandle((Handle)pH);
- }
- else {
- HUnlock((Handle)pH);
- gModPict = pH;
- result = true;
- GetClutBack(&sfr);
- }
- }
- }
- }
- FSClose(fileRef);
- }
- }
- return result;
- }
- /* =============================================================================================== */
- /* To allow the app to be used to view normal pictures it is going to
- continue even if the 'clut' is not found in the file.
- */
-
- void GetClutBack(SFReply *sfr) {
- short resNum, currRes;
- Handle resH;
-
- currRes = CurResFile();
- gSharedClut = nil; // no cheating allowed
-
- if ( (resNum = (HOpenResFile((sfr)->vRefNum, 0, (sfr)->fName, fsRdPerm))) == -1) {
- resNum = ResError();
- goto bail;
- }
- else {
- UseResFile(resNum);
- if ( resH = GetResource('clut', 128) ) {
- DetachResource(resH);
- gSharedClut = (CTabHandle) resH;
- }
- CloseResFile(resNum);
- }
- bail:
- UseResFile(currRes);
- }
-